package de.medizin.uni.halle.irm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

public class TextSettingsFragment extends SherlockFragment{

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    	final View v = inflater.inflate(R.layout.textsettings, container, false);
    	    	
    	SQLiteDatabase database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
      	Cursor c = database.rawQuery("SELECT * FROM settings WHERE setting='mailsubject';", null);
    	c.moveToFirst();
    	((EditText) v.findViewById(R.id.emailsubject)).setText(c.getString(c.getColumnIndex("parameter")));
  
    	c = database.rawQuery("SELECT * FROM settings WHERE setting='mailtext';", null);
    	c.moveToFirst();
    	((EditText) v.findViewById(R.id.emailcontent)).setText(c.getString(c.getColumnIndex("parameter")));
    	
    	c = database.rawQuery("SELECT * FROM settings WHERE setting='smstext';", null);
    	c.moveToFirst();
    	((EditText) v.findViewById(R.id.smscontent)).setText(c.getString(c.getColumnIndex("parameter")));
    	
		int count = 700 - ((EditText) v.findViewById(R.id.smscontent)).getText().length();
		((EditText) v.findViewById(R.id.smscount)).setText(String.valueOf(count));			
    	((EditText) v.findViewById(R.id.smscontent)).addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable arg0) {
				int count = 170 - ((EditText) v.findViewById(R.id.smscontent)).getText().length();
				((EditText) v.findViewById(R.id.smscount)).setText(String.valueOf(count));				
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			public void onTextChanged(CharSequence s, int start, int before, int count) {				
			}    		
    	});
    	
    	((Button) v.findViewById(R.id.save)).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
		    	SQLiteDatabase database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);

				ContentValues initialValues = new ContentValues();
				initialValues.put("setting", "mailsubject");
				initialValues.put("parameter",       ((EditText) v.findViewById(R.id.emailsubject)).getText().toString());
		    	database.replace("settings", null, initialValues);
		    	initialValues.clear();
				initialValues.put("setting", "mailtext");
				initialValues.put("parameter",   ((EditText) v.findViewById(R.id.emailcontent)).getText().toString());
		    	database.replace("settings", null, initialValues);
		    	initialValues.clear();
				initialValues.put("setting", "smstext");
				initialValues.put("parameter", ((EditText) v.findViewById(R.id.smscontent)).getText().toString());
		    	database.replace("settings", null, initialValues);

	    		Toast.makeText(getSherlockActivity(),"Einstellungen gespeichert", Toast.LENGTH_SHORT).show();
			}
		});    	
    	c.close();
    	database.close();
        return v;
    }
}
